mui.init({
})
mui.plusReady(function() {
	if(plus.webview.getWebviewById('paymentSuccess.html')) {
		plus.webview.getWebviewById('paymentSuccess.html').close('none')
	}

	var self = plus.webview.currentWebview();
	var confirm = new Vue({
		el: '#confirm',
		data: {
			imgCommonUrl: common.imageUrl,
			//订单号

			cancelMins: 0,
			cancelMinsText: 0,
			subNumber: self.parameter,
			userOrder: '',
			list: [],
			userAddressSub: ''
		},
		mounted: function() {
			orders()
		},
		methods: {
			submitOrder: function() {
				common.open('orderdetails.html', 'orderdetails.html', this.userOrder.subId)
			},
			cancel: function() {
				var btnArray = ['取消', '确定'];
				mui.confirm('是否取消订单', '', btnArray, function(e) {
					if(e.index == 1) {
						common.ajax('/userOrder/cancleOrder', {
							subNumber: self.parameter
						}, function(data) {
							if(data.code == 1) {
								var list = plus.webview.getWebviewById('../my/order.html')
								mui.fire(list, 'order');
								mui.back()
							} else {
								if(data.resultMsg) {
									common.toast(data.resultMsg)
								}
							}
						})
					}
				}, 'div')
			}
		},
		watch: {
			cancelMins: function(news, old) {
				this.cancelMinsText = secToTime(news)
			}
		},
	})

	function orders() {
		common.ajax('/userOrder/userOrder/' + self.parameter, {}, function(data) {
			if(data.code == 1) {
				var prodId = []
				var temp = []
				for(var i = 0; i < data.data.order.subOrderItemDtos.length; i++) {
					if(prodId.indexOf(data.data.order.subOrderItemDtos[i].prodId) < 0) {
						prodId.push(data.data.order.subOrderItemDtos[i].prodId)
						data.data.order.subOrderItemDtos[i].children = [data.data.order.subOrderItemDtos[i]]
						temp.push(data.data.order.subOrderItemDtos[i])
					} else {
						for(var c = 0; c < temp.length; c++) {
							if(temp[c].prodId == data.data.order.subOrderItemDtos[i].prodId) {
								temp[c].children.push(data.data.order.subOrderItemDtos[i])
							}
						}
					}
				}

				confirm.cancelMins = data.data.order.dueDate
				confirm.list = temp
				confirm.userOrder = data.data.order
				confirm.userAddressSub = data.data.order.userAddressSub
				down()
			} else {
				if(data.resultMsg) {
					mui.toast(data.resultMsg)
				}
			}
		})
	}

	function down() {
		if(confirm.cancelMins <= 0 && confirm.userOrder.status == 1) {
			mui.back()
			var list = plus.webview.getWebviewById('../my/order.html')
			mui.fire(list, 'order');
		} else {
			confirm.cancelMins--
				setTimeout(down, 1000)
		}
	}
	document.addEventListener('order', function() {
		orders()
	})

	/*时间格式*/
	var secToTime = function(s) {
		var t;
		if(s > -1) {
			var hour = Math.floor(s / 3600);
			var min = Math.floor(s / 60) % 60;
			var sec = s % 60;
			if(hour < 10) {
				t = '0' + hour + "时";
			} else {
				t = hour + "时";
			}

			if(min < 10) {
				t += "0";
			}
			t += min + "分";
			if(sec < 10) {
				t += "0";
			}
			t += sec.toFixed(0) + '秒';
		}
		return t;
	}

})